﻿using AutoMapper;
using HBKY.Shop.Common.Constant;
using HBKY.Shop.Common.Extension;
using HBKY.Shop.Entity;
using HBKY.Shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HBKY.Shop.Service
{
    /// <summary>
    /// 分页集合 映射viewModel 扩展方法
    /// </summary>
   public static class PageListExtension
    {
        public static PageList<V> MapToPageList<T,V>(this IMapper mapper, PageList<T>  pageList) where T : class, new()
        {
            var result = new PageList<V>()
            {
                PageSize = pageList.PageSize,
                PageCount = pageList.PageCount,
                TotalCount = pageList.TotalCount,
                Data = mapper.Map<List<V>>(pageList.Data)
            };
            return  result;
        }

        public static IEnumerable<TSource> ToPageList<TSource>(this IEnumerable<TSource> source, PageQuery page)
        {
           if (!source.HasContent() || page.PageIndex <= ApplicationConst.IntZero || page.PageSize <= ApplicationConst.IntZero) return default;
           return source.Skip((page.PageIndex - 1) * page.PageSize).Take(page.PageSize).Select(x => x).ToList();
        }
    }
}
